﻿using System;
using System.Collections;
using System.Text;
using System.Xml;
namespace Notepad_X.Classes
{
    public enum Strings {none, File, New, Open, OpenContaingFolder, Close, CloseAll, CloseOthers, Save, SaveAs, SaveAll, Recent, Clear, Print, PrintPreview, PageSetup, Exit, Edit, Undo, Redo, Copy,Cut, Paste, SelectAll, Find, NotFound, Next, Previous, Replace, GoTo, Tabs, Rename, Tools, Options, Favourites, Add,Organize, Highlight, Default, LineNumbers, Help, Information, About, BugReport, Website, FeutureRequest, Document, Plugins, Toolbars, Translation, Highlighters, Shortcuts, Shortcut, Language, Font, Select, Browse, Developers, Activate, Deactivate, Category, Up, Down, Remove, Apply, Cancel, Download, Update, ItemName, Name, Preview, SelectedInformation, Author, Description, Version, StatusBar, General, Icons, Extensions, CurrentVersion, LatestVersion, ReleaseNotes }

    public class Language
    {
        public Hashtable strings = new Hashtable();
        public string Author;
        public string Version;
        public string language;

        public String this[String Key]
        {
           get { return strings[Key].ToString();}
        }
        public String this[Strings Key]
        {
            get { return strings[Enum.GetName(typeof(Strings),Key)].ToString(); }
        }

        /// <summary>
        /// Set default language values and strings
        /// </summary>
        public void set_default()
        {
            language = "english";
            Author = "NohponeX";
            Version = Main.VersionName;

            strings.Clear();

            foreach (String key in Enum.GetNames(typeof(Strings)))
            {
                if (key != "none")
                {
                    strings.Add(key, DecodeDefaultKey(key));
                }
            }
        }
        /// <summary>
        /// Reads an language file and stores the translated strings into the collection
        /// </summary>
        /// <param name="path">Translation file path</param>
        public void ReadLanguageFile(string path)
        {
            XmlDocument xmlFile = new XmlDocument();
            xmlFile.Load(path);

            Author = xmlFile.SelectSingleNode("NotepadX/Information/Author").InnerText;
            Version = xmlFile.SelectSingleNode("NotepadX/Information/Version").InnerText;
            language = xmlFile.SelectSingleNode("NotepadX/Information/Language").InnerText;

            string id;
            foreach (XmlNode node in xmlFile.SelectNodes("NotepadX/String"))
            {
                id = node.Attributes.GetNamedItem("id").InnerText;
                if (!String.IsNullOrEmpty(node.InnerText))
                {
                    if (strings.Contains(id))
                    {
                        strings[id] = node.InnerText;
                    }
                }
            }
        }
        /// <summary>
        /// Returns an translated string
        /// </summary>
        /// <param name="value">The string key</param>
        public String GetString(Strings key)
        {
            return strings[Enum.GetName(typeof(Strings), key)].ToString();
        }
        /// <summary>
        /// Converts key values to Strings example: SaveAs to Save As
        /// </summary>
        public String DecodeDefaultKey(String key)
        {
            int length = key.Length;
            for (int i = 1; i < length; i++)
            {
                if (Char.IsUpper(key[i]))
                {
                    key = key.Insert(i, " ");
                    length += 1;
                    i += 1;
                }
            }
            return key;
        }
        /// <summary>
        /// Reads an language file and returns the properties as array (Language,Author,Version)
        /// </summary>
        /// <returns>String array (Name,Author,Version)</returns>
        /// <param name="path">Translation file path</param>
        public static string[] hashTranslation(string path)
        {
            string[] ret;
            try
            {
                XmlDocument xmlFile = new XmlDocument();
                xmlFile.Load(path);
                ret = new string[3];
                ret[0] = xmlFile.SelectSingleNode("NotepadX/Information/Language").InnerText;
                ret[1] = xmlFile.SelectSingleNode("NotepadX/Information/Author").InnerText;
                ret[2] = xmlFile.SelectSingleNode("NotepadX/Information/Version").InnerText;
            }
            catch (Exception)
            {
                ret = null;
            }
            return ret;
        }
    }
}
